﻿using LinqKit;
using System;
using System.Linq.Expressions;

namespace CommonUtils
{
    public static class LambdaUtil
    {
        public static Expression<Func<T, bool>> True<T>()
        => m => true;

        public static Expression<Func<T, bool>> And<T>(this Expression<Func<T, bool>> predicate1, bool condition,
        Expression<Func<T, bool>> predicate2)
        => condition ? predicate1.And(predicate2) : predicate1;

        [Obsolete]//IQueryable大概不支持Invoke函数
        public static Expression<Func<T, bool>> And<T>(this Expression<Func<T, bool>> predicate, Func<T, object> selector, object value)
        => value.IsEmpty() ? predicate : predicate.And(m => selector.Invoke(m) == value);

        public static bool IsEmpty<T>(this Expression<Func<T, bool>> predicate)
        => predicate.Body is ConstantExpression;
    }
}
